<html>
<head><meta charset="utf-8"><title>VariableSizedBox · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html">VariableSizedBox</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207593211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207593211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207593211">(Aug 21 2020 at 01:30)</a>:</h4>
<p>So in an effort to support the FFI pattern of structs that have variable sized arrays in them (also known as flexible array members to some people), I have created a <code>VariableSizedBox</code> type. I would greatly appreciate a thorough analysis to ensure it is sound according to stacked borrows.</p>
<p><a href="https://github.com/retep998/wio-rs/blob/master/src/vsb.rs">https://github.com/retep998/wio-rs/blob/master/src/vsb.rs</a></p>



<a name="207615409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207615409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Cook <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207615409">(Aug 21 2020 at 09:40)</a>:</h4>
<p>Off hand, zeroed should be unsafe, in case it creates an invalid struct. (Like if T is NonZeroU8)</p>



<a name="207663907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207663907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207663907">(Aug 21 2020 at 17:55)</a>:</h4>
<p>The validity of the data is asserted on access, not creation, so zeroed does not need to be unsafe.</p>



<a name="207664296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207664296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207664296">(Aug 21 2020 at 17:58)</a>:</h4>
<p>I thought it's UB to have the invalid data even if it's never accessed?</p>



<a name="207664724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207664724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207664724">(Aug 21 2020 at 18:02)</a>:</h4>
<p>Only if there's an actual <code>T</code> instance</p>



<a name="207664745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207664745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207664745">(Aug 21 2020 at 18:02)</a>:</h4>
<p>The code just allocates memory for a <code>T</code></p>



<a name="207665015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207665015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207665015">(Aug 21 2020 at 18:04)</a>:</h4>
<p>oh interesting</p>



<a name="207668354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207668354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207668354">(Aug 21 2020 at 18:33)</a>:</h4>
<p>yes, and to add a little more, any raw pointer to a T doesn't need to be aligned, or point to a valid address, or have valid bits at that address, until you <em>actually access</em> the pointer</p>



<a name="207721489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207721489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207721489">(Aug 22 2020 at 12:06)</a>:</h4>
<p>I haven't checked the entire API surface in detail, but generally this looks fine, yes. You can probably even make that raw ptr a <code>NonNull</code> if you want this box to have a niche.</p>



<a name="207824498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox/near/207824498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/VariableSizedBox.html#207824498">(Aug 24 2020 at 09:43)</a>:</h4>
<p>I updated it to use <code>NonNull</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>